package com.wlb365.admin.modules.devops.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wlb365.admin.common.exception.AdminException;
import com.wlb365.admin.modules.devops.entity.IsvRole;
import com.wlb365.admin.modules.devops.service.IsvRoleService;
import com.wlb365.admin.modules.devops.vo.IsvRoleTree;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.wlb365.admin.common.support.controller.BaseController;
import com.wlb365.admin.common.dto.R;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * isv角色 前端控制器
 * </p>
 *
 * @author
 * @since 2020-04-09 16:08:39
 */
@RestController
@RequestMapping("devops/isvRole")
public class IsvRoleController extends BaseController {

    @Autowired
    private IsvRoleService isvRoleService;

    @RequiresPermissions("devops:isvRole:index")
    @GetMapping("index")
    public ModelAndView index() {
        return new ModelAndView("devops/isv/isvRole");
    }

    @GetMapping(value = "edit")
    public ModelAndView edit(Long id) {
        ModelAndView mv = new ModelAndView("devops/isv/isvRole_edit");
        IsvRole isvRole;
        if (id == null) {
            isvRole = new IsvRole();
        } else {
            isvRole = isvRoleService.getById(id);
        }
        mv.addObject("editInfo", isvRole);
        return mv;
    }

    @GetMapping(value = "list/data")
    public R listData(IsvRole isvRole) {
        Page<IsvRole> page = isvRoleService.listIsvRolePage(isvRole);
        return R.ok(page);
    }

    @GetMapping(value = "list/tree")
    public R listTree() {
        List<IsvRole> list = isvRoleService.list();
        List<IsvRoleTree> roleTrees = new ArrayList<>();
        for (IsvRole role : list) {
            IsvRoleTree roleTree = new IsvRoleTree();
            BeanUtil.copyProperties(role, roleTree);
            roleTree.setParentId(0l);
            roleTree.setId(role.getRoleCode());
            roleTree.setName(role.getRoleName());
            roleTrees.add(roleTree);
        }

        return R.ok(roleTrees);
    }

    @RequiresPermissions("devops:isvRole:add")
    @PostMapping(value = "add")
    public R add(IsvRole isvRole) {
        List<IsvRole> list = isvRoleService.list(isvRoleService.lambdaQuery().eq(IsvRole::getRoleCode, isvRole.getRoleCode()));
        if (list != null && list.size() > 0) {
            throw new AdminException("角色编码已经存在！");
        }
        isvRoleService.save(isvRole);
        return R.ok();
    }

    @RequiresPermissions("devops:isvRole:del")
    @PostMapping(value = "batchdel")
    public R batchdel(@RequestParam("ids") List<Long> ids) {
        isvRoleService.removeByIds(ids);
        return R.ok();
    }

    @RequiresPermissions("devops:isvRole:edit")
    @PostMapping(value = "edit")
    public R edit(IsvRole isvRole) {
        isvRoleService.updateById(isvRole);
        return R.ok();
    }

    @RequiresPermissions("devops:isvRole:del")
    @PostMapping(value = "del/{id}")
    public R del(@PathVariable Long id) {
        isvRoleService.removeById(id);
        return R.ok();
    }

    @GetMapping(value = "get/{id}")
    public R get(@PathVariable Long id) {
        return R.ok(isvRoleService.getById(id));
    }

}

